package org.wabson.alfresco.translations;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Set;
import java.util.TreeSet;

public class ClassPathLoader implements BundleLoader
{
    /**
     * Base location
     */
    private String basePath;

    /**
     * Instantiate a new file system message loader with the given base location
     * 
     * @param fileName
     */
    public ClassPathLoader(String basePath)
    {
        this.basePath = basePath;
    }

    /* (non-Javadoc)
     * @see org.wabson.alfresco.translations.BundleLoader#exists(java.lang.String)
     */
    public boolean exists(String filePath)
    {
        URL url = ClassPathLoader.class.getResource(basePath + "/" + filePath);
        return url != null;
    }

    /* (non-Javadoc)
     * @see org.wabson.alfresco.translations.BundleLoader#load(java.lang.String)
     */
    public MessageBundle load(String filePath) throws BundleLoadingException
    {
        MessageBundle mb = new MessageBundle();
        // System.out.println("Reading in " + (basePath + File.separator +
        // filePath));
        BufferedReader in = new BufferedReader(new InputStreamReader(ClassPathLoader.class.getResourceAsStream(basePath + "/" + filePath)));

        String s;
        try
        {
            while ((s = in.readLine()) != null)
            {
                mb.addLine(s);
            }
        }
        catch (IOException e)
        {
            throw new BundleLoadingException(e);
        }
        return mb;
    }

    /* (non-Javadoc)
     * @see org.wabson.alfresco.translations.BundleLoader#findBundleFiles()
     */
    public Set<String> findBundleFiles()
    {
        return findBundleFiles(basePath, basePath, true);
    }

    /* (non-Javadoc)
     * @see org.wabson.alfresco.translations.BundleLoader#findBundleFiles(java.lang.String, java.lang.String, boolean)
     */
    public Set<String> findBundleFiles(String filePath, String basePath,
            boolean recurse)
    {
        Set<String> bundles = new TreeSet<String>();
        return bundles;
    }
    
    public String toString()
    {
        return "classpath:" + getBasePath();
    }

    public String getBasePath()
    {
        return basePath;
    }

	@Override
	public String getFileName()
	{
		return basePath.lastIndexOf("/") > -1 ? basePath.substring(basePath.lastIndexOf("/") + 1) : basePath;
	}

	@Override
	public int getNumFiles()
	{
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Set<String> getFiles()
	{
		// TODO Auto-generated method stub
		return null;
	}
}
